<?php 

if (isset($_GET['destroy'])) {
session_start();
session_destroy();

}
?>

<div data-role="view" data-title="RestFull Client" id="index" data-show="onIndexShow">    
    <header data-role="header">
        <div data-role="navbar">     
            
            <a data-align="left" data-icon="" data-role="button" href="Site/logout"  class="nav-button" >Logout</a>            
            
            <a data-align="left" data-icon="home" data-role="button"  data-rel="modalview" href="#modalview-add"></a>
            <span><?php //echo UserInformation::getLocationName(); ?></span>
            <a data-align="right" data-icon="refresh" data-role="button" class="nav-button" data-click='reload'></a>            
        </div>
    </header>
    <h3></h3>
    <div data-role="modalview" id="modalview-add" style="width: 100%; height: 100%;" >

        <div data-role="header">
            <div data-role="navbar">
                <span>Danh sách cửa hàng</span>
                <a data-click="closeModalViewAdd" data-role="button" data-align="right">x</a>
            </div>

        </div>

        <ul id="list-shop" data-role="listview" data-style="inset">
        </ul>
    </div>
    
    <div class="chart-wrapper">
                <div id="chart-home" style="height: 200px"></div>
    </div>


    <ul data-role="listview" data-style="inset" data-type="group">    	
        <li>    
            <?php
if(isset($status)){
    //Yii::app()->controller->refresh();
    //echo "99999999999999999999999";
    $url=Yii::app()->getHomeUrl();
    //echo $url;
    //echo $url;
    echo "<script>window.location.href = ".$url."</script>";
    //$this->redirect();	
}
?>
        	<ul>
            	<li ><a href="site/summary" >Doanh thu</a></li>
           	</ul>    	
                <ul>
            	<li ><a href="site/invoice" >Hóa đơn</a></li>
           	</ul>
           	<ul class="hide">
<!--            	<li  ><a href="site/order">Đơn hàng</a></li>
                </ul>
           	<ul class="hide">
            	<li ><a href="site/product">Hàng hóa</a></li>
                </ul>
           	<ul class="hide">
            	<li ><a href="site/customer">Khách hàng</a></li>-->
            </ul>   
                   
        </li>
    </ul>    
</div>
</div>

<script>
    var app = new kendo.mobile.Application(document.body, 
    {
        transition:'slide'
    });
    var i=1;
    
    function CurrencyFormatted(amount) {
                                    if(!amount) return 0;
                                    amount = Number((amount + '').replace(/[^\d]/g, ''));
                                    if(isNaN(amount)) return 0;
                                    amount = (amount + '').split('').reverse().join('').replace(/(\d{3})/g, '$1.').split('').reverse().join('');;
                                    if(amount[0] == '.')
                                     amount = amount.substr(1);
                                    return amount;
                                   }
    function reload()
    {
    	location.reload();
    }
   
    function onIndexShow()
    {
        
    	function createChart() {
                    $("#chart-home").kendoChart({
                    	dataSource: {
                            
                            transport: {
                                read: {
                                    url: "service/summaryWeekChart",
                                    dataType: "json"
                                }
                            },
                            sort: { 
                                field: "date",
                                dir: "asc"
                            }
                        },
                    	legend: {
                            position: "bottom"
                        },
                        series: [{
                            type: "line",
                            name: "Doanh số 7 ngày gần đây",
                            //field: "amount"
                            field: "amount"
                            
                        }],
                        categoryAxis: {
                        	field:"date",
                            labels: {
                                rotation: -90
                            }
                        },
                        valueAxis: {
                            line: {
                                visible: false
                            },
                            labels: {
                            format: "{0:C}",
                            template: "${CurrencyFormatted(value)}"
                            }
                        },
                        tooltip: {
                            visible: true,
                            format: "{0:C}",
                            template: "${CurrencyFormatted(value)}"
                        }
                        
                    });
                    window.addEventListener("load",function() {
                        // Set a timeout...
                        setTimeout(function(){
                                // Hide the address bar!
                                window.scrollTo(0, 1);
                            }, 0);
                    });
                    
                        $('.hide').hide();
                 
                     
                }
    	 createChart();
         loadShopList();
       
         $(window).resize(function() 
            {    
                var chart = $("#chart-home").data("kendoChart");
                chart.refresh();
            });
            
    }
   
    function closeModalViewAdd() {

        $("#modalview-add").kendoMobileModalView("close");

    }
    
    function loadShopList()
	    	{
	    		pageNumber = 0;	
	    		var dataSource = new kendo.data.DataSource({
	                serverPaging: true,
	                pageSize: 20,
	                transport: {
	                    read: {
	                        url: "service/shop", 
	                        dataType: "json" 
	                    },
	                    parameterMap: function(options) {
		                    if (paged) {	                        
		                         paged = false;
		                    }
		                    else
		                    {
		                    	pageNumber = 0;
		                    }
		                    options["page"] = pageNumber;
		                    return options;
		                }
	                },
	                schema: { // describe the result format
	                    parse: function(response) {
		                    pageNumber = pageNumber +1;
	                   
		                    return response;
		                },
	                    // the data which the data source will be bound to is in the "results" field
	                    data: function(data) {
	                        return data || [];
	                    },
	                    total: function() { return 10000; }
	                }
	            });
				paged = false;
		        $("#list-shop").kendoMobileListView({
		            dataSource: dataSource,
		            pullToRefresh: true,
		            appendOnRefresh: false,
		            endlessScroll: true,
		            endlessScrollParameters: function() {	            	
		                paged = true;
		            },
		            template: list_shop_item_template
		        });
                        //alert('end');
	    	}
    
    
    
    
    
    
</script>